"use strict";
cc._RF.push(module, '2de30WRowpJoKSiSAVQfu4n', 'GestureRecognize');
// res/scripts/game/Gesture/GestureRecognize.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DirectType = void 0;
var GestureConfig_1 = require("./GestureConfig");
//八个方位
var DirectType;
(function (DirectType) {
    DirectType[DirectType["Right"] = 1] = "Right";
    DirectType[DirectType["Right_Up"] = 2] = "Right_Up";
    DirectType[DirectType["Up"] = 3] = "Up";
    DirectType[DirectType["Left_Up"] = 4] = "Left_Up";
    DirectType[DirectType["Left"] = 5] = "Left";
    DirectType[DirectType["Left_Down"] = 6] = "Left_Down";
    DirectType[DirectType["Down"] = 7] = "Down";
    DirectType[DirectType["Right_Down"] = 8] = "Right_Down";
})(DirectType || (DirectType = {}));
exports.DirectType = DirectType;
var GestureRecognize = /** @class */ (function () {
    function GestureRecognize() {
        //最大误差
        this.gestureMaximumError = 10;
    }
    Object.defineProperty(GestureRecognize, "instance", {
        get: function () {
            if (!this._instance) {
                this._instance = new GestureRecognize();
            }
            return this._instance;
        },
        enumerable: false,
        configurable: true
    });
    // 计算方向
    GestureRecognize.prototype.getDirect = function (point1, point2) {
        var angle = Math.atan2(point2.y - point1.y, point2.x - point1.x) * 180 / Math.PI;
        if (angle < 0) {
            angle += 360;
        }
        // 根据角度范围确定方向
        if (angle >= 22.5 && angle < 67.5) {
            return DirectType.Right_Up;
        }
        else if (angle >= 67.5 && angle < 112.5) {
            return DirectType.Up;
        }
        else if (angle >= 112.5 && angle < 157.5) {
            return DirectType.Left_Up;
        }
        else if (angle >= 157.5 && angle < 202.5) {
            return DirectType.Left;
        }
        else if (angle >= 202.5 && angle < 247.5) {
            return DirectType.Left_Down;
        }
        else if (angle >= 247.5 && angle < 292.5) {
            return DirectType.Down;
        }
        else if (angle >= 292.5 && angle < 337.5) {
            return DirectType.Right_Down;
        }
        else {
            return DirectType.Right;
        }
    };
    // 计算两个数组之间的差异
    GestureRecognize.prototype.getArraySub = function (a1, a2) {
        var count = 0;
        for (var i = 0; i < a1.length; i++) {
            var item2 = a2[i] || 0;
            count += Math.abs(a1[i] - item2);
        }
        return count;
    };
    // 插值
    GestureRecognize.prototype.linearInterpolation = function (points, numPoints) {
        // 计算原始点之间的距离
        var distances = points.map(function (_, i) {
            if (i === points.length - 1)
                return 0;
            var dx = points[i + 1].x - points[i].x;
            var dy = points[i + 1].y - points[i].y;
            return Math.sqrt(dx * dx + dy * dy);
        });
        // 计算总距离
        var totalDistance = distances.reduce(function (a, b) { return a + b; }, 0);
        // 计算每个插值点之间的距离
        var stepDistance = totalDistance / (numPoints - 2);
        // 初始化插值点数组
        var interpolatedPoints = [];
        // 当前距离和当前点索引
        var currentDistance = 0;
        var currentIndex = 0;
        // 插值
        for (var i = 1; i < numPoints - 1; i++) {
            // 计算当前插值点在原始点数组中的位置
            while (currentDistance + distances[currentIndex] < stepDistance * i) {
                currentDistance += distances[currentIndex];
                currentIndex += 1;
            }
            // 计算当前插值点的位置
            var t = (stepDistance * i - currentDistance) / distances[currentIndex];
            var interpolatedPoint = {
                x: points[currentIndex].x + t * (points[currentIndex + 1].x - points[currentIndex].x),
                y: points[currentIndex].y + t * (points[currentIndex + 1].y - points[currentIndex].y)
            };
            // 添加到插值点数组
            interpolatedPoints.push(interpolatedPoint);
        }
        // 添加开头和结尾点
        interpolatedPoints.unshift(points[0]);
        interpolatedPoints.push(points[points.length - 1]);
        return interpolatedPoints;
    };
    // 计算方向列表
    GestureRecognize.prototype.getDirectList = function (points) {
        var directList = [];
        for (var i = 0; i < points.length - 1; i++) {
            directList.push(this.getDirect(points[i], points[i + 1]));
        }
        return directList;
    };
    // 手势识别
    GestureRecognize.prototype.matchGesture = function (direcList) {
        var min = 10000000;
        var gestureType = null;
        var gestureRec = GestureConfig_1.default.Instance.gestureRec;
        for (var key in gestureRec) {
            var gestureData = gestureRec[key];
            var configListArray = gestureData.direcListArray; // 获取手势配置列表
            for (var i = 0; i < configListArray.length; i++) {
                var configList = configListArray[i];
                var count = this.getArraySub(direcList, configList);
                if (count < min) {
                    min = count;
                    gestureType = key;
                }
            }
        }
        if (min > this.gestureMaximumError) {
            gestureType = null;
            console.log("没有匹配到手势");
            return null;
        }
        else {
            console.log("匹配到手势", gestureRec[gestureType].shape);
            return gestureRec[gestureType].shape;
        }
    };
    return GestureRecognize;
}());
exports.default = GestureRecognize;

cc._RF.pop();